﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Terraria.DataStructures;
using Terraria.ID;

namespace Terraria.GameContent.Tile_Entities
{
	// Token: 0x0200040E RID: 1038
	public class TETrainingDummy : TileEntity
	{
		// Token: 0x06002452 RID: 9298 RVA: 0x00019B4A File Offset: 0x00017D4A
		public static void Initialize()
		{
			TileEntity._UpdateStart += TETrainingDummy.ClearBoxes;
			TileEntity._NetPlaceEntity += TETrainingDummy.NetPlaceEntity;
		}

		// Token: 0x06002453 RID: 9299 RVA: 0x00019B6E File Offset: 0x00017D6E
		public static void NetPlaceEntity(int x, int y, int type)
		{
			if (type != 0)
			{
				return;
			}
			if (!TETrainingDummy.ValidTile(x, y))
			{
				return;
			}
			TETrainingDummy.Place(x, y);
		}

		// Token: 0x06002454 RID: 9300 RVA: 0x00019B86 File Offset: 0x00017D86
		public static void ClearBoxes()
		{
			TETrainingDummy.playerBox.Clear();
			TETrainingDummy.playerBoxFilled = false;
		}

		// Token: 0x06002455 RID: 9301 RVA: 0x0047D27C File Offset: 0x0047B47C
		public override void Update()
		{
			Rectangle rectangle = new Rectangle(0, 0, 32, 48);
			rectangle.Inflate(1600, 1600);
			int x = rectangle.X;
			int y = rectangle.Y;
			if (this.npc != -1)
			{
				if (!Main.npc[this.npc].active || Main.npc[this.npc].type != 488 || Main.npc[this.npc].ai[0] != (float)this.Position.X || Main.npc[this.npc].ai[1] != (float)this.Position.Y)
				{
					this.Deactivate();
					return;
				}
			}
			else
			{
				TETrainingDummy.FillPlayerHitboxes();
				rectangle.X = (int)(this.Position.X * 16) + x;
				rectangle.Y = (int)(this.Position.Y * 16) + y;
				bool flag = false;
				foreach (KeyValuePair<int, Rectangle> keyValuePair in TETrainingDummy.playerBox)
				{
					if (keyValuePair.Value.Intersects(rectangle))
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					this.Activate();
				}
			}
		}

		// Token: 0x06002456 RID: 9302 RVA: 0x0047D3CC File Offset: 0x0047B5CC
		private static void FillPlayerHitboxes()
		{
			if (!TETrainingDummy.playerBoxFilled)
			{
				for (int i = 0; i < 255; i++)
				{
					if (Main.player[i].active)
					{
						TETrainingDummy.playerBox[i] = Main.player[i].getRect();
					}
				}
				TETrainingDummy.playerBoxFilled = true;
			}
		}

		// Token: 0x06002457 RID: 9303 RVA: 0x0047D41C File Offset: 0x0047B61C
		public static bool ValidTile(int x, int y)
		{
			return Main.tile[x, y].active() && Main.tile[x, y].type == 378 && Main.tile[x, y].frameY == 0 && Main.tile[x, y].frameX % 36 == 0;
		}

		// Token: 0x06002458 RID: 9304 RVA: 0x00019B98 File Offset: 0x00017D98
		public TETrainingDummy()
		{
			this.npc = -1;
		}

		// Token: 0x06002459 RID: 9305 RVA: 0x0047D480 File Offset: 0x0047B680
		public static int Place(int x, int y)
		{
			TETrainingDummy tetrainingDummy = new TETrainingDummy();
			tetrainingDummy.Position = new Point16(x, y);
			tetrainingDummy.ID = TileEntity.AssignNewID();
			tetrainingDummy.type = 0;
			TileEntity.ByID[tetrainingDummy.ID] = tetrainingDummy;
			TileEntity.ByPosition[tetrainingDummy.Position] = tetrainingDummy;
			return tetrainingDummy.ID;
		}

		// Token: 0x0600245A RID: 9306 RVA: 0x0047D4DC File Offset: 0x0047B6DC
		public static int Hook_AfterPlacement(int x, int y, int type = 378, int style = 0, int direction = 1)
		{
			if (Main.netMode == 1)
			{
				NetMessage.SendTileSquare(Main.myPlayer, x - 1, y - 1, 3, TileChangeType.None);
				NetMessage.SendData(87, -1, -1, null, x - 1, (float)(y - 2), 0f, 0f, 0, 0, 0);
				return -1;
			}
			return TETrainingDummy.Place(x - 1, y - 2);
		}

		// Token: 0x0600245B RID: 9307 RVA: 0x0047D530 File Offset: 0x0047B730
		public static void Kill(int x, int y)
		{
			TileEntity tileEntity;
			if (TileEntity.ByPosition.TryGetValue(new Point16(x, y), out tileEntity) && tileEntity.type == 0)
			{
				TileEntity.ByID.Remove(tileEntity.ID);
				TileEntity.ByPosition.Remove(new Point16(x, y));
			}
		}

		// Token: 0x0600245C RID: 9308 RVA: 0x0047D580 File Offset: 0x0047B780
		public static int Find(int x, int y)
		{
			TileEntity tileEntity;
			if (TileEntity.ByPosition.TryGetValue(new Point16(x, y), out tileEntity) && tileEntity.type == 0)
			{
				return tileEntity.ID;
			}
			return -1;
		}

		// Token: 0x0600245D RID: 9309 RVA: 0x00019BA7 File Offset: 0x00017DA7
		public override void WriteExtraData(BinaryWriter writer, bool networkSend)
		{
			writer.Write((short)this.npc);
		}

		// Token: 0x0600245E RID: 9310 RVA: 0x00019BB6 File Offset: 0x00017DB6
		public override void ReadExtraData(BinaryReader reader, bool networkSend)
		{
			this.npc = (int)reader.ReadInt16();
		}

		// Token: 0x0600245F RID: 9311 RVA: 0x0047D5B4 File Offset: 0x0047B7B4
		public void Activate()
		{
			int num = NPC.NewNPC((int)(this.Position.X * 16 + 16), (int)(this.Position.Y * 16 + 48), 488, 100, 0f, 0f, 0f, 0f, 255);
			Main.npc[num].ai[0] = (float)this.Position.X;
			Main.npc[num].ai[1] = (float)this.Position.Y;
			Main.npc[num].netUpdate = true;
			this.npc = num;
			if (Main.netMode != 1)
			{
				NetMessage.SendData(86, -1, -1, null, this.ID, (float)this.Position.X, (float)this.Position.Y, 0f, 0, 0, 0);
			}
		}

		// Token: 0x06002460 RID: 9312 RVA: 0x0047D68C File Offset: 0x0047B88C
		public void Deactivate()
		{
			if (this.npc != -1)
			{
				Main.npc[this.npc].active = false;
			}
			this.npc = -1;
			if (Main.netMode != 1)
			{
				NetMessage.SendData(86, -1, -1, null, this.ID, (float)this.Position.X, (float)this.Position.Y, 0f, 0, 0, 0);
			}
		}

		// Token: 0x06002461 RID: 9313 RVA: 0x0047D6F4 File Offset: 0x0047B8F4
		public override string ToString()
		{
			return string.Concat(new object[]
			{
				this.Position.X,
				"x  ",
				this.Position.Y,
				"y npc: ",
				this.npc
			});
		}

		// Token: 0x0400403A RID: 16442
		private static Dictionary<int, Rectangle> playerBox = new Dictionary<int, Rectangle>();

		// Token: 0x0400403B RID: 16443
		private static bool playerBoxFilled = false;

		// Token: 0x0400403C RID: 16444
		public int npc;
	}
}
